<?php
/**
 * User: zyj
 * Date: 2019/11/12
 */

namespace app\manage\validate;


use think\Validate;

class Admin extends Validate
{
    protected $rule = [
        'username' => 'require|min:4|isName',
        'nickname' => 'require',
        'groupid'   => 'require|number',
        'password' => 'min:6|alphaDash',
    ];
    protected $message  =[
        'username.require' => '登录账号不能为空！',
        'username.min'     => '登录账号长度不能少于4位有效字符！',
        'username.isName'  => '登陆账号已存在请重新输入',
        'nickname.require' => '用户昵称不能为空',
        'groupid.require'   => '角色组必选',
        'groupid.number'    => '角色组选择错误',
        'password.alphaDash' => '登录密码只能是字母和数字,下划线_及破折号-',
        'password.min'     => '登录密码长度不能少于6位有效字符！',

    ];

    public function isName($value, $rule, $data=[])
    {
        $map = [];
        if(!empty($data['id'])){
            $map[] = ["id","<>",$data['id']];
        }
        $isName = \app\manage\model\Admin::where("username","=",$value)->where($map)->findOrEmpty();
        if(!$isName->isEmpty()){
            return false;
        }
        return true;
    }
}